﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Chizx.Web.user
{
    public partial class ChangePwd : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                string zu_id = "";
                if (Request.Params["id0"] != null && Request.Params["id0"].Trim() != "")
                {
                    zu_id = Request.Params["id0"];
                }
                int zc_id = -1;
                if (Request.Params["id1"] != null && Request.Params["id1"].Trim() != "")
                {
                    zc_id = (Convert.ToInt32(Request.Params["id1"]));
                }
                ShowInfo(zu_id, zc_id);
            }
        }

        private void ShowInfo(string zu_id, int zc_id)
        {
            Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
            Chizx.Model.UserModel model = bll.GetModel(zu_id, zc_id);
            this.lblzu_id.Text = model.zu_id;
            this.lblzc_id.Text = model.zc_id.ToString();
        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string zu_id = this.lblzu_id.Text;
            int zc_id = int.Parse(this.lblzc_id.Text);
            Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
            Chizx.Model.UserModel model = bll.GetModel(zu_id, zc_id);
            string strErr = "";
            if (this.txtzu_pwd.Text.Trim().Length == 0)
            {
                strErr += "zu_pwd不能为空！\\n";
            }
            if (this.txtzu_pwd.Text.Trim() != model.zu_pwd)
            {
                strErr += "原始密码不正确！\\n";
            }
            if (this.txtrezu_pwd.Text.Trim() != this.txtnewzu_pwd.Text.Trim())
            {
                strErr += "两次输入的密码不相同！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            model.zu_id = zu_id;
            model.zu_pwd = this.txtnewzu_pwd.Text.Trim();
            //写入系统日志
            if (Session["UserEntity"] == null)
            {
                Response.Redirect("/e/Login.aspx");
            }
            else
            {
                bll.Update(model);
                Chizx.Model.UserModel sbmmodel = (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = sbmmodel.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = "修改密码";
                slm.zl_dotype = "3";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                mm.zu_id = sbmmodel.zu_id;
                mm.zmsg_topic = "修改密码成功";
                mm.zmsg_context = "尊敬的用户 " + sbmmodel.zu_id + " ,您好！你已经成功修改了您的登录密码！新密码将在您下一次登录系统时生效！请在下一次使用新密码登录！";
                mm.zmsg_createdate = DateTime.Now;
                mm.zmsg_status = "m0";
                mb.Add(mm);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "/e/Hyber.aspx");
            }
            
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("/e/Hyber.aspx");
        }
    }
}
